package com.naiterui.ehp.bs.b2c.order.controller.inner;

import com.naiterui.ehp.bp.bo.agent.CustomOrderInfoBO;
import com.naiterui.ehp.bp.bo.b2c.OrderDeliveryBO;
import com.naiterui.ehp.bp.bo.b2c.UpdateOrderBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestHeaderDecode;
import com.naiterui.ehp.bs.b2c.inventory.exception.StockServiceException;
import com.naiterui.ehp.bs.b2c.order.exception.OrderBusinessException;
import com.naiterui.ehp.bs.b2c.order.exception.OrderExceptionCodes;
import com.naiterui.ehp.bs.b2c.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@Slf4j
@Api(tags = SwaggerTag.INNER_API + SwaggerTag.ORDER)
@Validated
@RestController
@RequestMapping("b2c/inner/cms/orders")
public class InnerOrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * h
     * 取消订单
     */
    @ApiOperation(value = "取消订单")
    @ApiImplicitParam(name = "orderIds", value = "订单ID集合", paramType = "body", dataType = "array", required = true)
    @PutMapping("/cancel")
    public ResponseVO<Void> cancel(@RequestBody Set<Long> orderIds, @RequestHeaderDecode String operator) throws BusinessException {
        log.info("CMS取消订单 请求参数：orderIds = {}, operator = {}", orderIds, operator);
        this.orderService.cancel(orderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * CMS审核订单
     */
    @ApiOperation(value = "CMS审核订单")
    @ApiImplicitParam(name = "orderIds", value = "订单ID集合", paramType = "body", dataType = "array", required = true)
    @PostMapping("/audit")
    public ResponseVO<Void> auditOrder(@RequestBody Set<Long> orderIds, @RequestHeaderDecode String operator) throws OrderBusinessException {
        log.info("CMS审核订单 请求参数：orderIds = {}, operator = {}", orderIds, operator);
        this.orderService.auditOrder(orderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * CMS反审核订单
     */
    @ApiOperation(value = "CMS反审核订单")
    @ApiImplicitParam(name = "orderIds", value = "订单ID集合", paramType = "body", dataType = "array", required = true)
    @PostMapping("/return/audit")
    public ResponseVO<Void> returnAuditOrder(@RequestBody Set<Long> orderIds, @RequestHeaderDecode String operator) {
        log.info("CMS反审核订单 请求参数：orderIds = {}, operator = {}", orderIds, operator);
        this.orderService.returnAuditOrder(orderIds, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }


    /**
     * 药店确认或者拒绝
     */
    @ApiOperation(value = "药店确认或者拒绝")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseId", value = "商家ID", paramType = "path", dataType = "long", required = true),
            @ApiImplicitParam(name = "orderIds", value = "订单ID集合", paramType = "body", dataType = "array", required = true),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "path", dataType = "long", required = true)})
    @PostMapping("/warehouse/{warehouseId}/{status}")
    public ResponseVO<Void> warehouseConfirmOrder(@PathVariable Long warehouseId, @RequestBody Set<Long> orderIds,
                                                  @RequestHeaderDecode String operator,
                                                  @PathVariable Integer status) throws OrderBusinessException {
        log.info("药店确认订单 请求参数：warehouseId = {} orderIds = {}, operator = {}, status = {}", warehouseId, orderIds, operator, status);
        this.orderService.warehouseConfirmOrder(warehouseId, orderIds, operator, status);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 药店发货
     */
    @ApiOperation(value = "药店发货")
    @ApiImplicitParam(name = "warehouseId", value = "商家ID", paramType = "path", dataType = "long", required = true)
    @PostMapping("/{warehouseId}/delivery")
    public ResponseVO<Void> delivery(@PathVariable Long warehouseId, @RequestBody Set<OrderDeliveryBO> deliveryInfo,
                                     @RequestHeaderDecode String operator) throws OrderBusinessException, StockServiceException {
        log.info("药店发货 请求参数：warehouseId = {}, deliveryInfo = {}, operator = {}", warehouseId, deliveryInfo, operator);
        this.orderService.deliveryOrder(warehouseId, deliveryInfo, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 修改订单
     */
    @ApiOperation(value = "修改订单")
    @PostMapping
    public ResponseVO<Void> updateOrder(@RequestBody UpdateOrderBO updateOrder,
                                        @RequestHeaderDecode String operator) throws BusinessException, StockServiceException {
        log.info("修改订单 请求参数：updateOrder = {}, operator = {}", updateOrder, operator);
        this.orderService.updateOrder(updateOrder, operator);
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS);
    }

    /**
     * 批量获取订单id
     *
     * @param recomIds
     * @return
     */
    @ApiOperation(value = "批量获取订单id")
    @ApiImplicitParam(name = "recomIds", value = "处方ID集合", paramType = "body", dataType = "array", required = true)
    @PostMapping("/getOrderId")
    public ResponseVO<Map<Long, Long>> getOrderId(@RequestBody Set<Long> recomIds) {
        return PublicService.returnResponseVO(OrderExceptionCodes.SUCCESS,
                this.orderService.getOrderByRecomIds(recomIds));
    }

    @ApiOperation(value = "取消订单")
    @ApiImplicitParam(name = "orderIds", value = "订单ID集合", paramType = "body", dataType = "array", required = true)
    @GetMapping("/getOrderDetailByOrderSn")
    public CustomOrderInfoBO getOrderDetailByOrderSn(String orderSn) throws BusinessException {
        return this.orderService.getOrderDetailByOrderSn(orderSn);
    }

}
